#include "StdAfx.h"
#include "PlayerController.h"
#include "PlayerControlLogic.h"
#include "EntityControl.h"
#include "PhysicsEntity.h"
#include "PlayerControlElement.h"
#include "ControlContext.h"
#include "ControlStatus.h"



PlayerController::PlayerController(PlayerControlLogic* playerControlLogic)
	:playerControlLogic(playerControlLogic)
{
}


PlayerController::~PlayerController(void)
{
	delete playerControlLogic;
}

bool PlayerController::isSpawningNeeded()
{
	return false;
}
void PlayerController::apply(EntityControl* entity, const ControlContext& context)
{

	//Move
	Move::IMoveManager* move = context.controlStatus.getController();
	Move::Quaternion oriMove = move->getOrientation(0);
	Ogre::Quaternion ori = Ogre::Quaternion(oriMove.w, oriMove.x, oriMove.y, oriMove.z);

	Move::Vector3 posMove = move->getPosition(0);
	Ogre::Vector3 pos = Ogre::Vector3(posMove.x, posMove.y, posMove.z);

	NxActor* actor = entity->getPhysicsActor()->getNxActor();
	Ogre::SceneNode* ogreNode = entity->getPhysicsActor()->getOgreNode();

	if(pos.x>30 || pos.x<-30 /*|| pos.y>50 || pos.y<-50*/)
	{
		entity->getPhysicsActor()->getOgreNode()->getParentSceneNode()->yaw(Ogre::Radian((-1.0*pos.x)/10000.0));
		//entity->getPhysicsActor()->getOgreNode()->getParentSceneNode()->pitch(Ogre::Radian((pos.y)/10000.0));
		//Ogre::Quaternion q = entity->getPhysicsActor()->getOgreNode()->getParentSceneNode()->getOrientation();
		//entity->getPhysicsActor()->getOgreNode()->getParentSceneNode()->setOrientation(q.w,q.x,q.y,0.0);
		
	}
	
	
	

	ogreNode->setOrientation(ori);
	ogreNode->setPosition(pos);

	Ogre::SceneNode* tempNode = ogreNode;
	pos = tempNode->_getDerivedPosition();
	Ogre::Quaternion orient = tempNode->_getDerivedOrientation();
	actor->moveGlobalPosition(NxVec3(pos.x,pos.y,pos.z));
	actor->moveGlobalOrientation(NxQuat(NxVec3(orient.x,orient.y,orient.z),orient.w));

	
	
	/*ogreNode->setOrientation(actor->getGlobalOrientationQuat().w,
				actor->getGlobalOrientationQuat().x,
				actor->getGlobalOrientationQuat().y,
				actor->getGlobalOrientationQuat().z);
		ogreNode->setPosition(actor->getGlobalPosition().x,
				actor->getGlobalPosition().y,
				actor->getGlobalPosition().z);*/


	
}



Ogre::String PlayerController::getType()
{
	return "PlayerController";
}